package cqrtplm.aop;

import com.hustcad.plm.pdm.doc.dto.CreateDocInputDTO;
import com.hustcad.plm.pdm.doc.dto.SaveDocDTO;
import com.hustcad.plm.pdm.doc.mapper.TyplmDocMasterMapper;
import com.hustcad.plm.pdm.doc.vo.DocumentVO;
import com.hustcad.plm.pdm.file.model.dto.ContentUploadDTO;
import com.hustcad.plm.pdm.file.model.dto.FileGroupDTO;
import cqrtplm.constant.RTErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yfq
 * @apiNote
 */

@Aspect
@Component
@Slf4j
public class IsDocumentNameExistAop {

    @Resource
    private TyplmDocMasterMapper typlmDocMasterMapper;

    @Around("execution(* com.hustcad.plm.pdm.doc.service.TyplmDocumentService.insertDoc(..))")
    public Object around(ProceedingJoinPoint point)throws Throwable{
        Object[] args = point.getArgs();
        if (!(args[0] instanceof SaveDocDTO)){
            return point.proceed();
        }
        SaveDocDTO saveDocDTO = (SaveDocDTO) args[0];
        FileGroupDTO fileGroupDto = saveDocDTO.getFileGroupDto();
        if(ObjectUtils.isEmpty(fileGroupDto)){
            return point.proceed();
        }
        CreateDocInputDTO createDocInputDTO = saveDocDTO.getCreateDocInputDTO();
        if(!"问题处理".equals(createDocInputDTO.getDocType())){
            List<MultipartFile> files = fileGroupDto.getFiles();
            if (CollectionUtils.isEmpty(files)){
                throw RTErrorCodeEnum.DOC_TYPE_ERROR.getException("请上传文档附件");
            }
            List<String> fileNameList = files.stream()
                    .map(MultipartFile::getOriginalFilename)
                    .filter(Objects::nonNull)
                    .map(name -> name.contains(".") ? name.substring(0, name.lastIndexOf('.')) : name)
                    .collect(Collectors.toList());
            String docName = String.join(",", fileNameList);
            //        List<String> docNameList = typlmDocMasterMapper.queryDocNames(docName);
            //        if (CollectionUtils.isNotEmpty(docNameList)){
            //            throw RTErrorCodeEnum.DOC_TYPE_ERROR.getException("文档名称已存在");
            //        }
            createDocInputDTO.setName(docName);
        }

        Object result = point.proceed(args);
        if (!(result instanceof DocumentVO)){
            return result;
        }
        DocumentVO documentVO = (DocumentVO) result;
        if(!"问题处理".equals(createDocInputDTO.getDocType())){
            List<ContentUploadDTO> contentUploadVOList = documentVO.getContentUploadVOList();
            if (CollectionUtils.isEmpty(contentUploadVOList)){
                throw RTErrorCodeEnum.DOC_TYPE_ERROR.getException("无文档附件");
            }
        }
        return result;
    }


}
